package com.bana.fvs.app.app;

import android.app.Application;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.bana.fvs.app.module.detaile.EquipmentDetailActivity;
import com.bana.fvs.app.mvp.ConstantsValue;
import com.wei.commonlibrary.manager.RxBus;
import com.wei.commonlibrary.utils.GlobalContext;
import com.wei.commonlibrary.utils.SPUtils;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * Author : yanftch
 * Date : 2018/2/6
 * Time : 11:13
 * Desc :
 */

public class MyApp extends Application {

    private static final String TAG = "debug_MyApp";

    private WebSocketClient webSocketClient;

    private int flag=0;

    private Handler handler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            initWebSocket();
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        GlobalContext.application = this;
        GlobalContext.context = getApplicationContext();
        initWebSocket();

        new Thread(){
            @Override
            public void run() {
                super.run();
                checkWebSocket();
            }
        }.start();
    }


    private void checkWebSocket(){
        while (true) {
            try {
                Thread.sleep(30 * 1000);

                if (webSocketClient != null && webSocketClient.isOpen()) {

                } else {
                    handler.sendEmptyMessageDelayed(0, 1000);
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void initWebSocket() {
        try {
            URI uri;
            if("".equals(SPUtils.getBaseUrl(this))){
                uri = new URI(ConstantsValue.SOCKET_VISIBILITY);
            }else {
                String url="ws://"+SPUtils.getBaseUrl(this)+":"+SPUtils.getBaseUrlPort(this)+"/bana_fvs/websocket/fogArea";
                uri = new URI(url);
            }
               webSocketClient = new WebSocketClient(uri) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                }

                @Override
                public void onMessage(String message) {
                    RxBus.getInstance().post(EquipmentDetailActivity.UPDATE_JCY,message);
                    Log.e(TAG, "73 ,onMessage:visibility message = " + message);
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    Log.e(TAG, "78 ,onClose: visibility =================== " + reason + "    code = " + code + "    remote = " + remote);
//                    if(flag<5){
//                        flag++;
//                        handler.sendEmptyMessageDelayed(0,10000*flag);
//                    }
                }

                @Override
                public void onError(Exception ex) {
                    Log.e(TAG, "83 ,onError: visibility e = " + ex.toString());
                }
            };
            webSocketClient.connect();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onTerminate() {
        super.onTerminate();
    }
}
